﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Uthorization.Read.API.Application.Command.MenuCommand;
using RBAC_Uthorization.Read.API.Application.Command.RoleCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Request;
using RBAC_Uthorization.Read.API.DTO.Response.Menu;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;


namespace RBAC_Uthorization.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class MenuController : ControllerBase
    {
       private readonly IMediator _mediator;

        public MenuController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 菜单显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PagingDto>> MenuShow(int pageIndex = 1, int pageSize = 10)
        {
            var entity = await _mediator.Send(new MenuShowCommand() { 
                pageIndex = pageIndex,
                pageSize = pageSize,
                offlc_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            });

            return entity;
        }
        /// <summary>
        /// 菜单树形显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenuTreeShowDto>>> MenuTreeShow()
        {
            var entity = await _mediator.Send(new MenuTreeShowCommand() {
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value

            });
            return entity;
        }
        /// <summary>
        /// 根据角色id获取菜单树
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenuTreeRoleIdShowDto>>> MenuTreeShowByRoleId()
        {
            var entity = await _mediator.Send(new MenuTreeRoleIdShowCommand() {
                create_user = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value,
                role_id = HttpContext.User.FindFirst(ClaimTypes.GroupSid)?.Value
            });
            return entity;
        }
        /// <summary>
        /// 根据菜单id获取操作权限
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenuOperationPidDto>>> MenuOperationPid(int pid)
        {
            var entity = await _mediator.Send(new MenuOperationPidCommand() { 
                Pid = pid,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            });
            return entity;
        }
        /// <summary>
        /// 菜单select显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenuSelectShow>>> MenuSelectShow()
        {
            var entity = await _mediator.Send(new MenuSelectShowCommand()
            {
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            });
            return entity;
        }
        /// <summary>
        /// 根据Id显示菜单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<MenuFindDto>> MenuFindId(int id)
        {
           
            var entity = await _mediator.Send(new MenuFindIdCommand()
            {
                id = id,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            });
            return entity;
        }
       
    }
}
